package com.dyz.swordFingerOffer;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author: daiyizheng
 * @date: 2021/5/31 13:27
 * @description:
 */
public class ReOrderArray21 {
    public static void main(String[] args) {
        int [] arr = new int[]{2,4,6,5,7};
        int[] ints = new ReOrderArray21().reOrderArray1(arr);
        System.out.println(Arrays.toString(ints));
    }

    public int[] reOrderArray (int[] array) {
        // write code here
        if(array==null || array.length==0){return array;}
        ArrayList<Integer> arr1 = new ArrayList();
        ArrayList<Integer> arr2 = new ArrayList();
        for (int i=0; i<array.length; i++){
            if(array[i]%2==0){
                arr2.add(array[i]);
            }else{
                arr1.add(array[i]);
            }
        }
        int [] arr0 = new int[array.length];
        for(int j=0; j<array.length;j++){
            if(j<arr1.size()){
                arr0[j]=arr1.get(j);
            }else{
                arr0[j]=arr2.get((j-arr1.size()));
            }
        }
        return arr0;
    }
    //插入排序的思想
    public int[] reOrderArray1 (int[] array) {
        if(array==null||array.length==0) {return array;}
        int j=0;//记录已经是奇数的位置
        int temp = 0;

        for(int i=0; i<array.length;i++){
            temp = array[i];
            if(array[i]%2==0){
                continue;
            }else {
                int k = i;
                while (k>j){
                    array[k] = array[k-1];//这区间整体向后移动一位
                    k--;
                }
                array[k] = temp;
                j++;
            }
        }
        return array;
    }
}
